<?php

namespace app\admin\controller\wechat;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 微信卡券投放页面
 *
 * @icon fa fa-circle-o
 */
class Cardpage extends Backend
{
    
    /**
     * WechatCardPage模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('WechatCardPage');
        $this->view->assign("canShareList", $this->model->getCanShareList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                if ($this->dataLimit)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                // 应用数据限制
                if ($this->appletLimit)
                {
                    $params['appletid'] = $this->appletLimit;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false){
                        $page = $this->model->get($this->model->id);
                        $wechatCard = new \app\mpbase\library\WechatCard();

                        $imgResult = $wechatCard->uploadImage('./'.$page['banner']);
                        if(!isset($imgResult['url'])){
                            $page->delete();
                            $this->error('图片上传失败');
                        }
                        $res = $wechatCard->createLandingPage(
                            $imgResult['url'],
                            $page['page_title'],
                            $page['can_share'],
                            $page['card_list']);
                        if($res['errcode'] == 0 && $res['errmsg'] == 'ok'){
                            $page->page_url = $res['url'];
                            $page->allowField(true)->save();
//                            $this->error('Debug ING.');
                            $this->success();
                        }else{
                            $page->delete();
                            $this->error($res['msg']);
                        }
                        $this->error('Debug ING...');
//                        $this->success();
                    }else{
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

}
